<?php
class Categories_m extends MY_Model{
	
	//public $_lang;
	//public $_lang_data;
	public $_table_lang = 'news_categories_lang';
	function __construct(){
		parent::__construct();
		$this->_table = 'news_categories';
		//$this->load->helper('lang');
		//$this->_lang = lang_current();
		//$this->_lang_data = lang_data(); // Array ( [0] => en [1] => vi ) 
//		echo $this->current_user->id;exit;
		
	}
	
	//lay de quy
	function get_all( $parent_id = 0 , $sym = '' ){
		//$this->_table .=' c';
		//$this->_table_lang .= ' l';
		$this->_table = 'news_categories c';
		$this->_table_lang = 'news_categories_lang l';
		
		$this->db->select('c.id, c.created, c.ordering,c.parent_id, l.title, l.slug, l.status');
		$this->db->order_by('c.ordering');
		$this->db->join($this->_table_lang,' l.id =  c.id');
		$this->db->where('l.lang',$this->_lang);
		$this->db->where('c.parent_id',$parent_id);
		$result =  parent::get_all();
		foreach($result as $item){
			$item->frefix = $sym;
			$item->child = $this->get_all($item->id,$sym.'►');
		}
		
		return $result;
	}
	
	function delete($id){
		//khi xoa thi update tat ca danh muc con cho parent_id = id
		$parent_id = parent::get($id)->parent_id;
		parent::delete($id);
		$this->db->where('parent_id',$id);//old
		$this->db->update($this->_table,array('parent_id'=>$parent_id));
		//xoa lang
		$this->_table = $this->_table_lang;
		parent::delete($id);
	}
	
	function ordering( $id, $model ){
		return parent::update($id,$model);
	}
	
	function get( $id ){
		$this->_table = 'news_categories c';
		$this->_table_lang = 'news_categories_lang l';
		$this->db->select('c.id, c.created, c.ordering,c.parent_id, l.title, l.slug, l.status');
		$this->db->join($this->_table_lang,' l.id =  c.id');
		$this->db->where('c.id',$id);
		$this->db->where('l.lang',$this->_lang);
		//return parent::get( $id );
		return $this->db->get($this->_table)->row();
	}
	
	
	function list_categories_radio( $root = true,$name = 'parent_id', $parent_id = 0, $remove_id = NULL, $check_id = NULL ){ // check_id parent_id
		$html = "";
		if($root)
			$html .= "<li><input type=\"radio\" name=\"$name\" value=\"0\" />Root<ul class=\"parent\">";
		if(isset($parent_id)){
			$this->db->where('parent_id',$parent_id);
		}
		$parent =  $this->get_all( $parent_id );
		
		foreach($parent as $item){
			$disabled = "";
			$class_cr = "";
			$checked = "";
			if($item->id == $remove_id){
				$disabled = " disabled ";
				
				$class_cr = "current";
			}
			if($item->parent_id == $parent_id){
				$checked = " p-checked ";
				
			}
			
			$html .= "<li class=\"".$class_cr.$checked."\" ><input ".$disabled." type=\"radio\" name=\"$name\" value=\"".$item->id."\" /><label>".$item->title."</label>";
			$html .= "<ul class=\"child1\">".$this->radio_child($name, $item->id, $remove_id, $check_id )."</ul></li>";
		}
		return $html.'</li>';
	}
	
	function radio_child( $name = 'parent_id', $parent_id ,$remove_id = NULL, $check_id = NULL  ){ // 0		
		$html = "";
		$this->db->where('parent_id',$parent_id);
		$parent =  $this->get_all($parent_id);
		foreach($parent as $item){

			$disabled = "";
			$class_cr = "";
			$checked = "";
			if($item->parent_id == $check_id && $item->id == $remove_id ){
				$disabled = " disabled ";
				$class_cr = " current ";
			}
			$html .= "<li class=\"".$class_cr.$disabled."\"><input $disabled type=\"radio\" name=\"$name\" value=\"".$item->id."\" /><label>".$item->title.'</label>';
			$html .= "<ul class=\"child2\">".$this->radio_child($name = 'parent_id', $item->id,$remove_id, $check_id )."</ul>";
			$html .= '</li>';
		}
		return $html.'';
	}
	
	
	//INSERT()
	function insert($data){
		//step1
		$cat['created'] = strtotime(date('Y-m-d H:i'));
		$cat['parent_id'] = $data['parent_id'] ? $data['parent_id'] : 0;
		$cat['create_by'] = $this->current_user->id;
		
		$id_insert = parent::insert($cat);
		//step2
		$this->_table = $this->_table_lang;
		if(isset($data['slug'])){
			$check = $this->db->where('slug',$data['slug'],'lang',$this->_lang)->get($this->_table)->row();
			if(count($check)>0)
				$data['slug'] .= "-".time();
		}
		else{
			$data['slug'] .= "-".time();
		}
		//insert multilang
		$lang_data['id'] = $id_insert;
		$lang_data['title'] = $data['title'];
		$lang_data['slug'] = $data['slug'];
		$lang_data['status'] = 1;
		foreach($this->_lang_data as $l){
			$lang_data['lang'] = $l;
			parent::insert($lang_data);
		}
		return $id_insert;
	}
	
	function update($id,$data){
		//step1 parent_id
		$this->db->where('id',$id);
		$cat['parent_id'] = $data['parent_id'];
		parent::update($id,$cat);
		//step2
		$this->_table = $this->_table_lang;
		if(isset($data['slug'])){
			$check = $this->db->where_not_in('id', $id)->where('slug',$data['slug'])->get($this->_table)->row();
			if(count($check)>0)
				$data['slug'] .= "-".time();
		}
		else{
			$data['slug'] .= "-".time();
		}
		$this->db->where('id',$id);
		$this->db->where('lang',$this->_lang);
		$lang_data['title'] = $data['title'];
		$lang_data['slug'] = $data['slug'];
		parent::update($id,$lang_data);
	}
}